package com.etoak.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {
    @Autowired
    StringRedisTemplate template;

    /**
     * String的set命令
     * @param key
     * @param value
     * @param timeUnit
     * @param expire
     */
    public void set(String key, String value, long expire,TimeUnit timeUnit) {
        template.opsForValue().set(key, value,expire,timeUnit);
    }

    /**
     * String的get命令
     *
     * @param key
     * @return
     */
    public String get(String key){
        return template.opsForValue().get(key);

    }
    public void hset(String key,String filed,String value){
        template.opsForHash().put(key,filed,value);
    }

    public List<String> hvals(String key){
         return template.opsForHash().values(key).stream().map(Object::toString).toList();
    }
    public void hdel (String key, String field){
        template.opsForHash().delete(key,field);
    }

    public void del(String key){
        template.delete(key);
    }

}
